package com.gxa.shop.service.order.impl;


import com.gxa.shop.bean.dto.OrderDto;
import com.gxa.shop.bean.pojo.*;
import com.gxa.shop.bean.vo.*;
import com.gxa.shop.commons.jdbc.JDBCUtil;
import com.gxa.shop.dao.cart.CartDao;
import com.gxa.shop.dao.cart.Impl.CartDaoImpl;
import com.gxa.shop.dao.order.OrderDao;
import com.gxa.shop.dao.order.impl.OrderDaoImpl;
import com.gxa.shop.service.order.OrderService;

import java.util.List;

/**
 * com.gxa.shop.service.order.impl
 * User: coma
 * Description:
 * Version: V1.0
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private CartDao cartDao = new CartDaoImpl();

    @Override
    public CheckInfoVo getCheckInfo(Integer userId, Integer addressId) {
        CheckInfoVo checkInfoVo = new CheckInfoVo();
        Address defaultAddress = orderDao.getAddressById(addressId);
        double postFee = 0.0;

        if (defaultAddress!=null && defaultAddress.getExpressfeeId()!=null) {
            // 获取运费规则
            Regin regin = orderDao.findRegionByExpressfeeId(defaultAddress.getExpressfeeId());
            if (regin != null) {
                postFee = regin.getReginMoney();
            } else {
                postFee = 0.0;
            }
        }

        // 从购物车获取商品信息
        List<CartVo> cartItems = cartDao.findById(userId.toString());
        double totalPeice = cartItems.stream().mapToDouble(item -> item.getUnitPrice() * item.getQuantity()).sum();
        double totalPayPrice = totalPeice + postFee;

        SummaryVo summaryVo = new SummaryVo();
        summaryVo.setGoodsCount(cartItems.size());
        summaryVo.setTotalPrice(totalPeice);
        summaryVo.setPostFee(postFee);
        summaryVo.setTotalPayPrice(totalPayPrice);

        checkInfoVo.setUserAddresses(orderDao.findUserAddresses(userId));
        checkInfoVo.setGoods(cartItems); // 使用购物车中的商品信息
        checkInfoVo.setSummary(summaryVo);

        return checkInfoVo;
    }
    @Override
    public Integer createOrder(Orders orders, List<OrderItems> orderItems) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        jdbcUtil.beginTransaction(); // 开始事务

        try {
            // 保存订单并获取生成的订单ID
            Integer orderId = orderDao.saveOrder(orders);

            if (orderId != null && orderId > 0) {
                // 保存订单项
                boolean itemsResult = orderDao.saveOrderItems(orderId, orderItems);

                jdbcUtil.endTransaction(true); // 提交事务

                // 确保两个操作都成功
                return orderId;
            } else {
                jdbcUtil.endTransaction(false); // 回滚事务
                return -1; // 表示创建失败
            }
        } catch (Exception e) {
            jdbcUtil.endTransaction(false); // 回滚事务

            // 记录异常日志（如果你有日志系统的话）
            e.printStackTrace(); // 或者使用更专业的日志记录方式

            return -1; // 表示创建失败
        }
    }

    @Override
    public List<Orders> getOrders(Integer orderId) {

       List<Orders> list=orderDao.findByOrderId(orderId);
       return list;
    }

    @Override
    public int updateOrder(OrderDto orderDto) {
        return orderDao.updateOrder(orderDto);
    }

    @Override
    public Long totalAllOrder(OrderDto orderDto) {
        return orderDao.getAllOrderTotal(orderDto);
    }

    @Override
    public List<OrderVo> selectOrder(OrderDto orderDto) {
        return orderDao.selectOrder(orderDto);
    }
    @Override
    public List<OrderMyVo> findMyOrder(Integer userId, Integer status, Integer page, Integer pageSize){
        return orderDao.getOrderInfo(userId,status,page,pageSize);
    }
    @Override
    public Integer totalOrder(Integer userId,Integer status){
        return orderDao.getTotalCount(userId,status);
    }
}